//
//  EPMEmotionTool.m
//  WeiBoByOC
//
//  Created by 马继鵬 on 17/4/7.
//  Copyright © 2017年 7TH. All rights reserved.
//

#import "EPMEmotionTool.h"


@interface EPMEmotionTool ()


@property (strong,nonatomic) NSArray<EPMEmotionModel*>* constentList;
@property (strong,nonatomic) NSArray<EPMEmotionModel*>* emojList;
@property (strong,nonatomic) NSArray<EPMEmotionModel*>* lxhList;
@end

static NSInteger emotionMaxCount = 20 ;

@implementation EPMEmotionTool
+ (instancetype)sheard{
    static EPMEmotionTool* tool;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [EPMEmotionTool new];
    });
    return tool;
}

- (NSBundle*)emotionBundle{
    if (_emotionBundle == nil){
         NSString* str = [[NSBundle mainBundle]pathForResource:@"Emoticons.bundle" ofType:nil];
        _emotionBundle = [NSBundle bundleWithPath:str];
    }
    return _emotionBundle;
}

- (NSMutableArray<EPMEmotionModel*>*)recentList{
    if (_recentList == nil){
        _recentList = [self getRecntEmotions];
    }
    return _recentList;
}


- ( NSArray<EPMEmotionModel*>*) constentList{
    
    return [self getEmotionArrayWithPath:@"default"];
}

- ( NSArray<EPMEmotionModel*>*) emojList{
    return [self getEmotionArrayWithPath:@"emoji"];
}
- ( NSArray<EPMEmotionModel*>*) lxhList{
    return [self getEmotionArrayWithPath:@"lxh"];
}

- (NSMutableArray*)allEmotions{
    
    NSMutableArray* temp = [NSMutableArray new];
    NSMutableArray* arr = [NSMutableArray new];
    [arr addObject:self.recentList];
    [temp addObject:arr];
    [temp addObject:[self getEmotionGroupWithEmotionList:self.constentList]];
    [temp addObject:[self getEmotionGroupWithEmotionList:self.emojList]];
    [temp addObject:[self getEmotionGroupWithEmotionList:self.lxhList]];
    return temp.mutableCopy;
}

#pragma
#pragma-获取数组
- (NSArray<EPMEmotionModel*>*)getEmotionArrayWithPath:(NSString*)file{
    
    NSString* filePath = [self.emotionBundle pathForResource:[NSString stringWithFormat:@"%@/info.plist",file] ofType:nil];
    
    NSArray* temp = [NSArray arrayWithContentsOfFile:filePath];
    
    NSArray<EPMEmotionModel*>* emotionList = [NSArray yy_modelArrayWithClass:[EPMEmotionModel class] json:temp];
    for ( EPMEmotionModel* model in emotionList){
        model.allPath = [NSString stringWithFormat:@"%@/%@",file,model.png];
    }
    
    return emotionList;
}

#pragma
#pragma-包装二维数组
- (NSArray*)getEmotionGroupWithEmotionList:(NSArray*)emotionList{
    NSInteger pageCount = (emotionList.count + emotionMaxCount - 1)/emotionMaxCount;
    NSMutableArray* tempArr = [NSMutableArray new];
    
    for (int i = 0; i < pageCount;i ++){
        
        NSInteger location = i * pageCount ;
        NSInteger length = emotionMaxCount;
        if ( location + length > emotionList.count){
            length = emotionList.count % emotionMaxCount;
        }
        
        NSRange range = NSMakeRange(location, length);
        
        NSArray* temp = [emotionList subarrayWithRange:range];
        [tempArr addObject:temp];
    }
    return tempArr.copy;
}


#pragma
#pragma-存储最近表情
- (void)saveRecentEmotions:(EPMEmotionModel*)model{
    //遍历数组中的表情是否有相同的
    if (model.isEmoji){
        for (EPMEmotionModel* obj in self.recentList){
            if(obj.code == model.code){
                [self.recentList removeObject:obj];
                //加速
                break;
            }
        }
    }else{
        for(EPMEmotionModel* obj in self.recentList){
            if (obj.png == model.png){
                [self.recentList removeObject:model];
                //加速
                break;
            }
        }
    }
    //存储
    [self.recentList insertObject:model atIndex:0];
    if (self.recentList.count >= 20){
        [self.recentList removeLastObject];
    }
    self.allEmotions[0][0] = self.recentList;
    [NSKeyedArchiver archiveRootObject:self.recentList toFile:EMOTIONFILEPATH];
}

- (NSMutableArray*)getRecntEmotions{
    
    NSArray* arr = [NSKeyedUnarchiver unarchiveObjectWithFile:EMOTIONFILEPATH];
    if (arr == nil){
        arr = [NSArray new];
    }
    return arr.mutableCopy;
}

- (EPMEmotionModel*)getEmotionModel:(NSString*)chs{
    
    for (EPMEmotionModel* model in self.constentList ){
        if (model.chs == chs){
            return model;
        }
        
    }
    
    for (EPMEmotionModel* model in self.lxhList){
        if (model.chs == chs){
            return model;
        }
    }
    
    return [EPMEmotionModel new];
}
@end
